package com.jt.www.common.service.impl;

/**
 * @author admin_ltf
 * @create 2019-06-17 16:02
 */

import com.jt.www.common.service.PolicyAttributeService;
import com.jt.www.dao.mapper.policyattribute.PolicyAttributeEntityMapper;
import com.jt.www.model.policyattribute.PolicyAttributeEntity;
import com.jt.www.model.policyattribute.vo.PlicyClassVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.weekend.Weekend;
import tk.mybatis.mapper.weekend.WeekendCriteria;

import java.util.ArrayList;
import java.util.List;

@Service
public class PolicyAttributeServiceImpl implements PolicyAttributeService {

    @Autowired
    PolicyAttributeEntityMapper policyAttributeEntityMapper;

    public List<PlicyClassVO> getProInfo(String channel, String type, String pid) throws Exception {

        List<PlicyClassVO> resultList = new ArrayList<>();
        PlicyClassVO plicyClassVO = null;
        PolicyAttributeEntity policyAttributeEntity = new PolicyAttributeEntity();
        policyAttributeEntity.setChannel(channel);
        policyAttributeEntity.setType(type);
        if (!StringUtils.isEmpty(pid)) {
            policyAttributeEntity.setParentId(Long.valueOf(pid));
        }
        policyAttributeEntity.setIsDeleted(false);
        List<PolicyAttributeEntity> policyAttributeEntityList = policyAttributeEntityMapper.select(policyAttributeEntity);
        if (policyAttributeEntityList != null && policyAttributeEntityList.size() > 0) {
            for (PolicyAttributeEntity p : policyAttributeEntityList) {
                plicyClassVO = new PlicyClassVO();
                String name = p.getName();
                String code = p.getCode();
                Long id = p.getId();
                plicyClassVO.setId(id.toString());
                plicyClassVO.setCode(code);
                plicyClassVO.setName(name);
                resultList.add(plicyClassVO);
            }
        }
        return resultList;
    }

    @Override
    public List<PolicyAttributeEntity> getAttributes(String channel, String type, String code) {
        Weekend<PolicyAttributeEntity> weekend = new Weekend<>(PolicyAttributeEntity.class);
        WeekendCriteria<PolicyAttributeEntity, Object> criteria = weekend.weekendCriteria();
        criteria.andEqualTo(PolicyAttributeEntity::getChannel, channel);
        if (StringUtils.isNotBlank(type)) {
            criteria.andEqualTo(PolicyAttributeEntity::getType, type);
        }
        if (StringUtils.isNotBlank(code)) {
            criteria.andEqualTo(PolicyAttributeEntity::getCode, code);
        }
        criteria.andEqualTo(PolicyAttributeEntity::getIsDeleted, false);

        return policyAttributeEntityMapper.selectByExample(weekend);
    }
}
